<?php
/**
 * 商户后台--商品管理控制器
 * @author 孙银川
 * @date   2017.12.30
 */
namespace app\index\controller;

use think\facade\Session;
use app\common\model\DataExport;
use app\common\model\Goods as GoodsModel;
use think\Request;

class Goods extends Base
{

    /**
     * 商品分类列表
     */
    public function cateList()
    {
        // 商户ID
        $shop_id = Session::get('sp_id','index');
        if(!$shop_id) $this->error('请先登录','index/login');
        $gmodel = new GoodsModel();
        $map[] = ['sg_status', '<', 2];
        $cates = $gmodel->cates($shop_id, $map);

        $this->assign('cates', $cates);
        return $this->fetch('goods/cateList');
    }

    /**
     * 添加/修改商品分类
     */
    public function cateUpd()
    {
        // 商户ID
        $shop_id = Session::get('sp_id','index');
        // 分类ID
        $id = input('get.id');
        $gmodel = new GoodsModel();

        // 添加分类
        if (input('get.act') == 'a') {
            $data = input('post.');
            if ($data['sg_name']) {
                $data['sg_shop_id'] = $shop_id;
                $data['sg_create_time'] = time();
                $res = $gmodel->add_cate($data);
                if ($res) {
                    $this->success('添加成功', 'cateList');
                } else {
                    $this->error('添加失败', 'cateList');
                }
            }
            // 修改分类
        } elseif (input('get.act') == 'u') {
            $data = input('post.');
            if ($data['sg_id']) {
                $res = $gmodel->upd_cate($data);
                if ($res) {
                    $this->success('修改成功', 'cateList');
                } else {
                    $this->error('修改失败', 'cateList');
                }
            }
        } elseif ($id) {
            $cate = $gmodel->cate($id);
            $this->assign('cate', $cate);
        }

        return $this->fetch('goods/cateUpd');
    }

    /**
     * ajax删除商品分类
     */
    public function cateAjax()
    {
        if (input('get.id')) {
            $data['sg_id'] = input('get.id');
            $data['sg_status'] = 2;
            $gmodel = new GoodsModel();
            $res = $gmodel->upd_cate($data);
            return $res;
        }
    }

    /**
     * 添加商品
     */
    public function goodsAdd()
    {
        // 商户ID
        $shop_id = Session::get('sp_id','index');
        if(!$shop_id) $this->error('请先登录','index/login');
        $gmodel = new GoodsModel();
        // 提交表单
        if (input('get.act') == 'a') {
            $data = input('post.');
            // 商品价格(转换成分)
            if ($data['gd_price']) {
                $data['gd_price'] = $data['gd_price']*100;
            }
            // 商品原价(转换成分)
            if ($data['gd_old_price']) {
                $data['gd_old_price'] = $data['gd_old_price']*100;
            }
            // 商户ID
            if ($shop_id) {
                $data['gd_shop_id'] = $shop_id;
            }

            // 添加时间
            if ($data['gd_name']) {
                $data['gd_create_time'] = time();
            }

            // 图片上传
            $file = $this->request->file('image');
            if ($file) {
                $info = $file->move("public/upload/goods");
                if ($info) {
                    $save_name = $info->getPathName();
                    if ($save_name) {
                        $data['gd_image_url'] = $save_name;
                    }

                } else {
                    echo $file->getError();
                }
            }
            // 执行添加
            $res = $gmodel->add($data);
            if ($res) {
                $this->success('添加成功', 'goodsList');
            } else {
                $this->error('添加失败', 'goodsList');
            }
        } else {
            // 获取所有商品分类
            $cates = db('shop_gcate')->where('sg_shop_id=' . $shop_id . ' and sg_status<2')->select();
            $this->assign('cates', $cates);
        }

        return $this->fetch('goods/goodsAdd');
    }

    /**
     * 商品列表
     */
    public function goodsList()
    {
        // 商户ID
        $shop_id = Session::get('sp_id','index');
        if(!$shop_id) $this->error('请先登录','index/login');
        $gmodel = new GoodsModel();
        // 未删除
        $map['gd_status'] = ['g.gd_status','<',2];
        if ($shop_id) {
            $map['gd_shop_id'] = ['g.gd_shop_id', '=', $shop_id];
        }

        // 搜索
        if ((input('get.act') == 's') && ($name = input('post.name'))) {
            // 查询条件
            $map['gd_name'] = ['g.gd_name', 'like', '%' . $name . '%'];
            $data = $gmodel->enquiries(10, $map);
        } else {
            $data = $gmodel->enquiries(10, $map);
        }
        $goods_list = $data->items();
        foreach ($goods_list as $k => $v) {
            // 商品状态
            if ($v['gd_status'] == 1) {
                $goods_list[$k]['gd_status'] = '上架';
            } else {
                $goods_list[$k]['gd_status'] = '下架';
            }
            // 审核状态
            switch ($v['gd_audit']) {
                case 0:$goods_list[$k]['gd_audit'] = '待审核';
                    break;
                case -1:$goods_list[$k]['gd_audit'] = '未通过';
                    break;
                case 1:$goods_list[$k]['gd_audit'] = '通过';
                    break;
            }
            // 上架时间
            $goods_list[$k]['gd_sale_time'] = date('Y-m-d H:i:s', $v['gd_sale_time']);
            // 添加时间
            $goods_list[$k]['gd_create_time'] = date('Y-m-d H:i:s', $v['gd_create_time']);
        }

        // 导出excel表格
        if (input('get.act') == 'ep') {
            $dmodel = new DataExport();
            // 标题行
            $title = array('商品ID', '商品名称', '商品分类', '商品价格', '商品状态', '审核状态', '店铺名称', '上架时间', '添加时间');
            $res = $dmodel->export($goods_list, $title, '商品数据表格');
        }

        $this->assign('page', $data->page);
        $this->assign('count', $data->count);
        $this->assign('goods_list', $goods_list);
        return $this->fetch('goods/goodsList');
    }

    /**
     * 商品详情
     */
    public function goodsDetail()
    {
        $id = input('get.id');
        // 获取商户信息
        $gmodel = new GoodsModel();
        if ($id) {
            $goods = $gmodel->detail($id);
            $this->assign('goods', $goods);
        }

        return $this->fetch('goods/goodsDetail');
    }

    /**
     * 修改商品
     */
    public function goodsUpd()
    {
        // 商户ID
        $shop_id = Session::get('sp_id','index');
        if(!$shop_id) $this->error('请先登录','index/login');
        $id = input('get.id');
        $gmodel = new GoodsModel();
        if (input('get.act') == 'u') {
            $data = input('post.');

            // 图片上传
            $file = $this->request->file('image');
            if ($file) {
                $info = $file->move("public/upload/goods");
                if ($info) {
                    $save_name = $info->getPathName();
                    if ($save_name) {
                        $data['gd_image_url'] = $save_name;
                    }

                } else {
                    echo $file->getError();
                }
            }
            if ($data['gd_price']) {
                $data['gd_price'] = $data['gd_price']*100;
            }
            if ($data['gd_old_price']) {
                $data['gd_old_price'] = $data['gd_old_price']*100;
            }
            // 执行添加
            $res = $gmodel->edit($data);
            if ($res) {
                $this->success('修改成功', 'goodsList');
            } else {
                $this->error('修改失败', 'goodsList');
            }
        } else {
            // 获取所有商品分类
            $cates = db('shop_gcate')->where('sg_shop_id=' . $shop_id . ' and sg_status<2')->select();
            // 获取商品信息
            $goods = $gmodel->detail($id);

            $this->assign('cates', $cates);
            $this->assign('goods', $goods);
        }

        return $this->fetch('goods/goodsUpd');
    }

    /**
     * ajax删除商品 status=2
     */
    public function goodsAjax()
    {
        if(input('get.id')){
            $data['gd_id'] = input('get.id');
            $data['gd_status'] = 2;
            $gmodel = new GoodsModel();
            $res = $gmodel->edit($data);
            return $res;
        }else{
            return 0;
        }
    }
}
